package top.wu2020.lazy.ws.chat.infrastructure.persistence;

import top.wu2020.lazy.ws.chat.infrastructure.entity.CommunityDO;
import top.wu2020.lazy.ws.chat.infrastructure.converter.CommunityConverter;
import top.wu2020.lazy.ws.chat.infrastructure.mapper.CommunityMapper;
import top.wu2020.lazy.ws.chat.domain.model.community.CommunityRepository;
import org.springframework.stereotype.Repository;
import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.community.Community;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe community 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence 
 **/
@Repository
public class CommunityRepositoryImpl  implements  CommunityRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增
     *
     * @param community 新增     
     * @return {@link Result<Community>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> story(Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        lazyLambdaStream.upsert(communityDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param communityList 批量新增     
     * @return {@link Result<List<Community>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Community>> batchStory(List<Community> communityList) {
        List<CommunityDO> communityDOList = communityList.stream().map(CommunityConverter.INSTANCE::fromCommunity).collect(Collectors.toList());
        lazyLambdaStream.upsert(communityDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param community 查询单个     
     * @return {@link Result<Community>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> findOne(Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        Community communityOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(communityDO), Community.class);
        return ResultFactory.successOf(communityOne);
    }

    /**
     * describe 查询多个
     *
     * @param community 查询多个     
     * @return {@link Result<List<Community>>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Community>> findList(Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        List<Community> communityList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(communityDO), Community.class);
        return ResultFactory.successOf(communityList);
    }

    /**
     * describe 分页查询多个
     *
     * @param size 当前页数
     * @param current 当前页
     * @param community 分页查询多个     
     * @return {@link Result<LazyPage<Community>>} 分页领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<Community>> findPage(int size,int current,Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        LazyPage<Community> lazyPage = new LazyPage<>(current,size);
        LazyPage<Community> communityLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(communityDO),lazyPage, Community.class);
        return ResultFactory.successOf(communityLazyPage);
    }

    /**
     * describe 删除
     *
     * @param community 删除     
     * @return {@link Result<Community>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> remove(Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(communityDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param community 领域对象     
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Boolean> exists(Community community) {
        CommunityDO communityDO = CommunityConverter.INSTANCE.fromCommunity(community);
        Boolean exists=lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(communityDO));
        return ResultFactory.successOf(exists);
    }

}